package sw.serial;

public class SmithWaterman_Serial  {
    private String U, V, AlignU, AlignV;
	private int d, ma, mi; 
    
	public SmithWaterman_Serial(String U, String V) {
		this.U = U;
		this.V = V;
	}
  
    private int sigma( char u, char v ){
        return u == v ? ma : mi;
    }
        
    public void alignment(){
        int n, m, i, j;
        int match, delete, insert;
        d = -1;
		ma = 2;
		mi = -1;
		if (U.length() < V.length()) {
			String swap = V;
			V = U;
			U = swap;
		}
		
        n = U.length();
        m = V.length();
        
        int M[][] = new int[n+1][m+1];

        // reset column 0 only
        for ( i = 0; i < n+1; i++ )
            M[i][0] = 0;
        
        // reset row 0 only
        for ( j = 0; j < m+1; j++ )
            M[0][j] = 0;
        long before = System.nanoTime();
        for ( i = 1; i < n+1; i++ ){
            for ( j = 1; j < m+1; j++ ){
                match = M[i-1][j-1] + sigma( U.charAt(i-1), V.charAt(j-1) );
                delete = M[i-1][j] + d;
                insert = M[i][j-1] + d;
                M[i][j] = Math.max( Math.max(Math.max(0,match),delete), insert );
            }
        }
        long after = System.nanoTime();
        float totalDurationMs = (after-before)/1e6f;
		System.out.println("Total duration Of Serial: "+totalDurationMs+"ms");
		
    }
    
    public void printMatrix(int[][] M, int n, int m) {
    	for ( int i = 0 ; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    			System.out.print(M[j][i] + " ");
    		}
    		System.out.println("");
    	}
    }
}
